<template>
<div>
    <AutoQuery ref="auto-query" :query="query" :page="page"></AutoQuery>
    <AutoTable :columns="table.columns" :data="articleList" :page="page"></AutoTable>
    <Modal v-model="modal1" title="编辑文档" :width="80" :mask-closable="false">
        <HelpPost v-model="current_id" @on-add="handleModalAdd" @on-modify="handleModalModify"></HelpPost>
        <div slot="footer"></div>
    </Modal>
</div>
</template>
<script>
import Page from "@/common/model/Page";
import AutoQuery from "@/common/components/AutoQuery";
import AutoTable from "@/common/components/AutoTable";
import HelpDocument from "@/common/model/HelpDocument"
import HelpPost from "./help_post";
import parent from "../Parent";

export default {
    extends:parent,
    components: {
        AutoQuery, AutoTable, HelpPost
    },
    data(){
        return{
            test: [],
            current_id: 0,
            modal1: false,
            page: new Page("search/help_document"), //分页查询接口
            query: { // 查询条件
                type: { type: "select", label: "文档类别：", default: "*", placeholder: "请选择文档类别", options: [] },
                search: { type: "input", label: "标题：", placeholder: "按标题搜索" },
                begin_time: { type: "date", label: "日期：", placeholder: "选择开始时间" },
                end_time: { type: "date", placeholder: "选择截止时间", handleValue: value => { return value + 24*60*60*1000 } },
            },
            table: {
                columns: [
                    {
                        title: '序号', width: 65, align: 'center', render: (h, params) => {
                            return h('div', (this.page.current_page - 1) * this.page.page_size + params.index + 1);
                        }
                    },
                    {
                        title: '标题', key: 'title'
                    },
                    {
                        title: '类别', width: 200, align: 'center', key: 'hd_type',
                    },
                    {
                        title: '发布日期', width: 150, align: 'center', key: 'formated_create_time',
                        render: (h, params) => {
                        return h('div', params.row.formated_create_time);
                    }
                    },
                    {
                        title: '操作', key: 'action', width: 200, align: 'center', render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '5px',display:this.showButton("110813") },
                                    on: { click: () => { this.handleEdit(params.row) } }
                                }, '修改'),
                                h('Button', {
                                    props: { type: params.row.status == 0 ? 'ghost' : 'info', size: 'small' },
                                    style: { marginRight: '5px',display:this.showButton("110819") },
                                    on: { click: () => { this.handleStatus(params.row) } }
                                }, params.row.status == 0 ? '已隐藏' : '已公开'),
                                h('Button', {
                                    props: { type: 'error', size: 'small' },
                                    style: { marginRight: '5px',display:this.showButton("110812") },
                                    on: { click: () => { this.handleDelete(params.row) } }
                                }, '删除')
                            ]);
                        }
                    }
                ]
            }
        }
    },
    computed:{
        articleList(){
            return HelpDocument.parseList(this.page.list);
        },
        chosen_type () {
            return this.$store.state.app.dicTree.help.children;
        }
    },
    methods:{
        handleAdd() {
            this.current_id = 0;
            this.modal1 = true;
        },
        handleEdit(row) {
            this.current_id = row.id;
            this.modal1 = true;
        },
        handleModalAdd(elem) {
            this.page.current_page = 1;
            this.modal1 = false;
            this.page.getData();
        },
        handleModalModify(elem) {
            this.modal1 = false;
            this.page.getData();
        },
        handleStatus(row) {
            this.api.post("help/status", { id: row.id, status: 1 - row.status }, res => {
                row.status = 1 - row.status;
            });
        },
        handleDelete(row) {
            if(!confirm("删除不可恢复，您确认要删除吗？")) return;
            this.api.post("help/delete", { id: row.id }, res => {
                this.page.getData();
            });
        }
    },
    created() {
        this.process("dic_tree", () => {
            var temp = this.$store.state.app.dicTree.help.children.map( elem => {
                    return { value: elem.name, label: elem.name };
                });
            this.query.type.options = [{ value: "*", label: "全部" }].concat(temp);
        });
    }
};
</script>